package com.example.myapp.ADO;



import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp.Model.Msg;
import com.example.myapp.Model.User;
import com.example.myapp.R;
import com.loopj.android.image.SmartImageView;

import java.util.List;

import cn.bmob.v3.BmobUser;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{
    private List<Msg> mMsgList;
    private String OtherIconUrl;

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rihgtMsg;
        private SmartImageView msg_myicon;
        private SmartImageView msg_othericon;
        public ViewHolder(View view) {
            super(view);
            leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
            leftMsg = (TextView) view.findViewById(R.id.left_msg);
            rihgtMsg = (TextView) view.findViewById(R.id.right_msg);
            msg_myicon =view.findViewById(R.id.msg_myicon);
            msg_othericon=view.findViewById(R.id.msg_othericon);
            msg_othericon.setImageUrl(OtherIconUrl);
            msg_myicon.setImageUrl(BmobUser.getCurrentUser(User.class).getIcon().getUrl());


        }
    }

    public  MsgAdapter(List<Msg> msgList,String otherIconUrl) {
        mMsgList = msgList;
        OtherIconUrl=otherIconUrl;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int i) {
        String userID = mMsgList.get(i).getAuthor();
        if (userID.equals(BmobUser.getCurrentUser(User.class).getObjectId())) {
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rihgtMsg.setText(mMsgList.get(i).getContent());
        } else {
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(mMsgList.get(i).getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}